package Com.javaserver.utils;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DB {

    // 数据库DRIVER
    private final static String CLASS_DRIVER = "com.mysql.cj.jdbc.Driver";
    // 数据库地址，修改域名、端口、数据库名称
    private final static String DATABASE_URL = "jdbc:mysql://izpop.cn/uki?serverTimezone=GMT%2B8&characterEncoding=utf-8";
    // 数据库账号
    private final static String DATABASE_USER = "uki";
    // 数据库密码
    private final static String DATABASE_PASSWORD = "123456";

    static {
        try {
            Class.forName(CLASS_DRIVER);//加载数据库驱动
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private DB() {
    }

    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(DATABASE_URL, DATABASE_USER, DATABASE_PASSWORD);//连接数据库
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void closeConnection(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void closeStatement(Statement stmt) {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行查询语句
     *
     * @param sql  要执行的sql，可以传入 ? 作为参数替换
     * @param args 任意个参数
     * @return List<Map>，map内为每一行的参数
     */


    //显示数据用这个!
    public static List<Map<String, Object>> executeQuery(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            List<Map<String, Object>> result = new ArrayList<>();
            while (resultSet.next()) {
                Map<String, Object> temp = new HashMap<>();
                int columnCount = resultSet.getMetaData().getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    temp.put(resultSet.getMetaData().getColumnLabel(i), resultSet.getObject(i));
                }
                result.add(temp);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            closeStatement(preparedStatement);
            closeConnection(connection);
        }
    }

    /**
     * 执行更新语句
     *
     * @param sql  要执行的sql，可以传入 ? 作为参数替换
     * @param args 参数列表
     * @return 语句影响的数据行数
     */
    public static int executeUpdate(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }
            return preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            closeStatement(preparedStatement);
            closeConnection(connection);
        }
    }
    public static void main(String[] args) {
        DB h=new DB();
        System.out.println(h);
    }


}